package de.puc.defencetower.view.gui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import de.puc.defencetower.controller.iGameController;
import de.puc.defencetower.controller.action.BuildTowerAction;
import de.puc.defencetower.controller.action.UpgradeTowerAction;
import de.puc.defencetower.controller.exception.TowerBuildException;
import de.puc.defencetower.controller.exception.TowerUpgradeException;
import de.puc.defencetower.model.Map;
import de.puc.defencetower.model.MapPosition;
import de.puc.defencetower.model.Tower;
import de.puc.defencetower.model.towers.TowerBig;
import de.puc.defencetower.model.towers.TowerMiddle;
import de.puc.defencetower.model.towers.TowerSmall;
import de.puc.defencetower.view.gui.GamePanel;

@SuppressWarnings("serial")
public class MenuPanel extends GamePanel implements ActionListener {

	private JButton buttonBuildSmallTower;
	private JButton buttonBuildMiddleTower;
	private JButton buttonBuildBigTower;
	private JButton buttonSpawn; 
	private JButton buttonUpgrade;

	public MenuPanel(Map map, iGameController gameControlerInterface,
			GUIWindow gameWindow) {
		super(map, gameControlerInterface, gameWindow);
		setBackground(Color.white);
		init();
	}

	private void init() {
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

		buttonSpawn = new JButton("Spawn next Wave");
		buttonSpawn.addActionListener(this);
		add(buttonSpawn);
		
		buttonUpgrade = new JButton("Upgrade"); 
		buttonUpgrade.addActionListener(this); 
		add(buttonUpgrade);

		buttonBuildSmallTower = new JButton("SmallTower (" + TowerSmall.GOLDCOSTS+"g)");
		buttonBuildSmallTower.addActionListener(this);
		add(buttonBuildSmallTower);

		buttonBuildMiddleTower = new JButton("MiddleTower (" + TowerMiddle.GOLDCOSTS + "g)");
		buttonBuildMiddleTower.addActionListener(this);
		add(buttonBuildMiddleTower);

		buttonBuildBigTower = new JButton("BigTower (" + TowerBig.GOLDCOSTS + "g)");
		buttonBuildBigTower.addActionListener(this);
		add(buttonBuildBigTower);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		MapPosition lastClicked = getGameWindow().getMapPanel() .getLastClicked();
		
		// Tower bauen Button
		int type = -1;
		if (source.equals(buttonBuildSmallTower)) {
			type = Tower.TOWERTYPE_SMALL;
		} else if (source.equals(buttonBuildMiddleTower)) {
			type = Tower.TOWERTYPE_MIDDLE;
		} else if (source.equals(buttonBuildBigTower)) {
			type = Tower.TOWERTYPE_BIG;
		}
		if (type != -1) {
			try {
				getGameControlerInterface().buildTower(new BuildTowerAction(lastClicked.getX(), lastClicked.getY(), type, this));
			} catch (TowerBuildException eB) {
				JOptionPane.showMessageDialog(this, eB.getMessage());
			}
		} else if (source.equals(buttonSpawn)) {
			getGameControlerInterface().spwanNextWave();
		} else if (source.equals(buttonUpgrade)){
			try {
				getGameControlerInterface().upgradeTower(new UpgradeTowerAction(lastClicked.getX(), lastClicked.getY(), this, getMap()));
			} catch (TowerUpgradeException eB) {
				JOptionPane.showMessageDialog(this, eB.getMessage());
			}
		}

	}

	public void refresh() {

	}
}
